
/***  DOCUMENTATION LAYER

	Bafflegate Javascript Tracker

	Last Update: Sep 2007
	Author: Tom at klenwell@gmail.com

SUMMARY	
	Collects user info, assembles a query string, and calls bafflegate log server 
  php file to log data in bafflegate database
  
  See baffleticket.tpl.htm for sample of code snippet that would be pasted into
  a tracked page.

NOTES
  Uses js tag -- any reason to use iframe (or another) instead?
  
  To link: <script type="text/javascript" src="path/to/file.js"></script>

______________________________________________________________________________*/

// Log Server Settings

  // this is the url where the bafflegate log file is located
  // replace %baffledomain% with correct url/path
  var bafflelog_url = 'http://%baffledomain%/log.php';

  
// Data we want to collect

  var is_framed = 0;
  var dst_url = '';
  var ref_url = '';
  var page_title = '';
  var agent_name = '';
  var agent_ver = 0.0;
  var agent_str = '';
  var agent_os = '';  
  var screen_size = '';
  var screen_depth = 0;

  
// Are we being framed?
if ( window.top != window.self ) is_framed = 1;

// Get destination url (i.e. page we're tracking -- a fallback if $_SERVER['HTTP_REFERER'] fails
if ( typeof(document.location) != 'undefined' ) dst_url = document.location;
else if ( typeof(document.URL) != 'undefined' ) dst_url = document.URL;
  
// Get referrer
if ( !is_framed )
{
  ref_url = document.referrer;
}
else
{
  ref_url = window.top.document.referrer;
  frame_url = window.top.location;
}

// Get title
page_title = document.title;

   
// Get agent info
if ( typeof(navigator.appName) != 'undefined' ) agent_name = navigator.appName;
if ( typeof(navigator.appVersion) != 'undefined' ) agent_ver = navigator.appVersion;
if ( typeof(navigator.userAgent) != 'undefined' ) agent_str = navigator.userAgent;
if ( typeof(navigator.platform) != 'undefined' ) agent_os = navigator.platform;
if ( agent_os == '' && typeof(navigator.oscpu) != 'undefined' ) agent_os = navigator.oscpu;


// Screen info
screen_size = screen.width + "x" + screen.height;
 
if ( typeof(screen.colorDepth) != 'undefined' ) 
{
  screen_depth = screen.colorDepth;
}
else if ( typeof(screen.pixelDepth) != 'undefined' ) 
{
  screen_depth = screen.colorDepth;
}



// bafflegate logger function / assembles query and calls file
function bafflelog()
{
  var qstring = '';
  var qa = '&';
  
  // build query string
  var q1 = 'baffleticket=' + baffleticket + qa;
  var q2 = 'ref=' + escape(ref_url) + qa;
  var q3 = 'ttl=' + escape(page_title) + qa;
  var q4 = 'aos=' + escape(agent_os) + qa;
  var q5 = 'ssz=' + screen_size + qa;
  var q6 = 'sdp=' + screen_depth + qa;
  var q7 = 'frm=' + is_framed + qa;
  var q8 = 'url=' + escape(dst_url) + qa;
  var q9 = 'jsa=1';
  
  qstring = '?' + q1  + q2 + q3 + q4 + q5 + q6 + q7 + q8 + q9;
  
  // logger server override?
  if ( typeof(bafflelog_url_override) != 'undefined' ) bafflelog_url = bafflelog_url_override;
  
  // build src and payload tag (is js or iframe tag better?)
  var src = bafflelog_url + qstring;
  var js_tag = '<script language="JavaScript" type="text/javascript" src="' + src + '"></script>';
  //var iframe_tag = '<iframe src="'+ src +'" height="1" width="1" frameborder="0" scrolling="no"></iframe>';
  
  // debug
  //alert(src);
  
  // call php logger on server by tag
  document.open();
  document.write(js_tag);
  document.close();  
}

